import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuController {
    public static void showMainMenu(JFrame frame, String loggedInUser, String userRole) {
        frame.getContentPane().removeAll();
        frame.getContentPane().revalidate();
        frame.getContentPane().repaint();

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1));



        JButton communityServiceButton = new JButton("社区服务");
        JButton communityMallButton = new JButton("社区商城");
        JButton communityInteractionButton = new JButton("社区互动");
        JButton adminManagementButton = new JButton("后台管理");
        JButton userManagementButton = new JButton("用户管理");
        JButton userInfoButton = new JButton("个人信息");
        JButton logoutButton = new JButton("切换账号");

        panel.add(communityServiceButton);
        panel.add(communityMallButton);
        panel.add(communityInteractionButton);

        if ("创建者".equals(userRole) || "管理员".equals(userRole)) {
            panel.add(adminManagementButton);
            panel.add(userManagementButton);

        } else {
            panel.add(userInfoButton);
        }

        panel.add(logoutButton);

        communityServiceButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "进入社区服务界面...");
                // 这里可以添加社区服务的具体逻辑
            }
        });

        communityMallButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "进入社区商城界面...");
                // 这里可以添加社区商城的具体逻辑
            }
        });

        communityInteractionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "进入社区互动界面...");
                // 这里可以添加社区互动的具体逻辑
            }
        });

        adminManagementButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "进入后台管理界面...");
                // 这里可以添加后台管理的具体逻辑
            }
        });

        userManagementButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ("创建者".equals(userRole) || "管理者".equals(userRole)) {
                    LoginController.showUserManagementFrame(frame, loggedInUser, userRole);
                } else {
                    JOptionPane.showMessageDialog(frame, "没有权限访问用户管理界面.");
                }
            }
        });

        userInfoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "用户名: " + loggedInUser + "\n角色: " + userRole);
            }
        });

        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().removeAll();
                frame.getContentPane().revalidate();
                frame.getContentPane().repaint();
                LoginController.showLoginFrame(frame);
            }
        });

        frame.getContentPane().add(panel);
        frame.revalidate();
        frame.repaint();
    }
}